<?php

/**
 * items actions.
 *
 * @package    sf_sandbox
 * @subpackage items
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class itemsActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    if($request->getParameter('box'))
    {
      $this->getUser()->setParameter('removal',$request->getParameter('removal'));
    }

    $this->redirect('boxes/index');
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->item = Doctrine_Core::getTable('Item')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->item);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new ItemForm();
    $this->icos = $this->getIcos();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new ItemForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($item = Doctrine_Core::getTable('Item')->find(array($request->getParameter('id'))), sprintf('Object item does not exist (%s).', $request->getParameter('id')));
    $this->form = new ItemForm($item);
    $this->icos = $this->getIcos();
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($item = Doctrine_Core::getTable('Item')->find(array($request->getParameter('id'))), sprintf('Object item does not exist (%s).', $request->getParameter('id')));
    $this->form = new ItemForm($item);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($item = Doctrine_Core::getTable('Item')->find(array($request->getParameter('id'))), sprintf('Object item does not exist (%s).', $request->getParameter('id')));
    $item->delete();

    $this->redirect('items/index');
  }
  
  public function executeIcoupload(sfWebRequest $request)
  {
	$fileNames = $this->getRequest()->getFiles('icoFile');
	if(!move_uploaded_file($fileNames['tmp_name'],sfConfig::get('sf_web_dir').'/images/ico_items/'.$fileNames['name']))
	{
		exit(-1);
	}

	$this->redirect('items/new');    
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
	
	  $form->setRemovalId($this->getUser()->getAttribute('removal'));
      $item = $form->save();

      $this->redirect('items/edit?id='.$item->getId());
    }
  }

  protected function getIcos()
  {
    $dirname = dirname(__FILE__).'/../../../../../web/images/ico_items/';
    $dir = opendir($dirname); 
    $icos= array();
    while($file = readdir($dir)) {
      if($file != '.' && $file != '..' && !is_dir($dirname.$file))  $icos[] = $file;
    }
    return $icos;
  }
}
